package com.programmingteam.wallpaperslideshow.service;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.programmingteam.wallpaperslideshow.contentprovider.ImageProvider.ImagesTable;
import com.programmingteam.wallpaperslideshow.contentprovider.ImageProvider.SearchResultsTable;
import com.programmingteam.wallpaperslideshow.imagesearch.PhotoData;

public class CacheManager {

	ContentResolver mContentResolver;

	public CacheManager(Context c) {
		mContentResolver = c.getContentResolver();
	}

	public Uri cacheImage(PhotoData photoData) {
		String deleteWhere;
		ContentValues values;

		//Delete this image from cache if alredy exists
		deleteWhere = ImagesTable.PHOTO_ID + "=" + photoData.getImageId() + " and " +
				ImagesTable.PHOTO_PROVIDER + "='" + photoData.getProvider() + "'";
		mContentResolver.delete(ImagesTable.CONTENT_URI, deleteWhere, null);

		//Add new image to cache
		values = new ContentValues();
		values.put(ImagesTable.PHOTO_ID, photoData.getImageId());
		values.put(ImagesTable.PHOTO_PROVIDER, photoData.getProvider());
		values.put(ImagesTable.PHOTO_FILE, photoData.getFile().getAbsolutePath());
		values.put(ImagesTable.PHOTO_PAGE_URI, "" + photoData.getSourcePageUri());
		values.put(ImagesTable.PHOTO_URI, "" + photoData.getImageUri());
		values.put(ImagesTable.PHOTO_DESC, photoData.getPhtotoDescription());
		values.put(ImagesTable.PHOTO_PLACE, photoData.getPhotoNearPlace());
		return mContentResolver.insert(ImagesTable.CONTENT_URI, values);
	}

	public void clearCache(int numThumbsRemain, int numInfoRemain) {
		int numToClearThumbs, numToClearInfo, curr = 0;
		Cursor c;
		File thumb;
		List<Long> idToDelete, idToNullFile;
		Long imageId;
		String s;

		c = mContentResolver.query(ImagesTable.CONTENT_URI, null, null, null, "_ID asc");
		numToClearThumbs = c.getCount() - numThumbsRemain;
		numToClearInfo = c.getCount() - numInfoRemain;
		idToDelete = new LinkedList<Long>();
		idToNullFile = new LinkedList<Long>();
		if (c.moveToFirst() && numToClearThumbs > 0) {
			do {
				s = c.getString(c.getColumnIndex(ImagesTable.PHOTO_FILE));
				if (s != null) {
					thumb = new File(s);
					thumb.delete();
					imageId = c.getLong(c.getColumnIndex(ImagesTable._ID));
					idToNullFile.add(imageId);

					if (curr < numToClearInfo) {
						idToDelete.add(imageId);
					}
				}
				++curr;
			}
			while (c.moveToNext() && curr < numToClearThumbs);
		}
		c.close();

		// TODO change where to IN
		for (Long id : idToDelete) {
			mContentResolver.delete(ImagesTable.CONTENT_URI, ImagesTable._ID + "=" + id, null);
		}

		// TODO change where to IN
		for (Long id : idToNullFile) {
			ContentValues cv = new ContentValues(1);
			cv.putNull(ImagesTable.PHOTO_FILE);
			mContentResolver.update(ImagesTable.CONTENT_URI, cv, ImagesTable._ID + "=" + id, null);
		}
	}

	public int destroyCache() {
		int numDeleted;
		numDeleted = mContentResolver.delete(ImagesTable.CONTENT_URI, "1=1", null)
				+ mContentResolver.delete(SearchResultsTable.CONTENT_URI, "1=1", null);
		return numDeleted;
	}
}
